#include "StdAfx.h"
#include "CBoardCell2D.h"
#include "CFolderTree2D.h"

//#define WHITE RGB(255, 16, 255)
#define GRAY RGB(236, 233, 216)

BEGIN_MESSAGE_MAP(CBoardCell2D, CWnd)
    ON_WM_PAINT()
    ON_WM_CREATE()
    ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()

CBoardCell2D::CBoardCell2D(void)
{
}

CBoardCell2D::CBoardCell2D(int nCellID/*, Color CellColor, int nWidth, int nHeight*/)
	:CBoardCell(nCellID/*, CellColor, nWidth, nHeight*/)
{
	/*m_initColor			= CellColor;*/
	m_hoverColor		= Color::Ivory;
	m_mouseClickColor	= Color::BlueViolet;
}

CBoardCell2D::~CBoardCell2D(void)
{
	GdiplusShutdown(m_gdiplusToken);
}

void CBoardCell2D::setInitColor( Color initColor )
{
}

void CBoardCell2D::setHoverColor( Color hoverColor )
{
}

void CBoardCell2D::setMouseClickColor( Color mouseClickColor )
{
}

Color CBoardCell2D::getInitColor(  )
{
	return m_initColor;
}

Color CBoardCell2D::getHoverColor(  )
{
	return m_hoverColor;
}

Color CBoardCell2D::getMouseClickColor(  )
{
	return m_mouseClickColor;
}

BOOL CBoardCell2D::isClicked()
{
	return m_bClicked;
}

void CBoardCell2D::PreSubclassWindow()
{
	CStatic::PreSubclassWindow();
	ModifyStyle(0, SS_OWNERDRAW);
}

/*****************************************************************************
* ++
* Method name   :    int CBoardCell2D::OnCreate(LPCREATESTRUCT lpCreateStruct)
* Description   :    On event Create handler. This function will be called
*	when the window is created
*
* Parameters    :  LPCREATESTRUCT lpCreateStruct (not use)
* Return values :    
* Mod. History  :    03/12/2012 - BaoTD1 (first created)
*                    
* --
*****************************************************************************/
int CBoardCell2D::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);

    return(0);
}

/*****************************************************************************
 * ++
 * Method name   :    void CBoardCell2D::OnPaint(void)
 * Description   :    On event Paint handler. This function will be called
 *	when the WM_PAIN is created
 *
 * Parameters    : 
 * Return values :    
 * Mod. History  :    03/12/2012 - BaoTD1 (first created)
 *					  08/06/2013 - NhatLD (remove unnecessary code)
 *
 * --
 *****************************************************************************/
void CBoardCell2D::OnPaint(void)
{
	CPaintDC dc(this); // Creates a device context for the client area, which
	// also erases the area to be drawn.
	CRect rect;
	GetClientRect(rect);

	HDC Memhdc;
	HBITMAP Membitmap;
	CRect MainRect(rect);

	// Double Buffer
	Memhdc = CreateCompatibleDC(dc.m_hDC);
	Membitmap = CreateCompatibleBitmap(dc.m_hDC, MainRect.Width() , MainRect.Height());
	SelectObject(Memhdc, Membitmap);

	CRect MemRect(0,0, MainRect.Width(), MainRect.Height());
	FillRect(Memhdc, MemRect, CreateSolidBrush(WHITE));
	Rectangle(Memhdc,MemRect.left, MemRect.top,MemRect.right, MemRect.bottom);
	// GDI+
	Graphics graphics(Memhdc);
	graphics.SetSmoothingMode(SmoothingModeHighQuality);

	// Draw Cell
	DrawCell(&graphics, rect);

	BitBlt(dc, MainRect.left, MainRect.top, MainRect.Width(), MainRect.Height(), Memhdc, 0, 0, SRCCOPY);
	DeleteObject(Membitmap);
	DeleteDC    (Memhdc);
	DeleteDC    (dc);
}

/******************************************************************************
* ++
* Method name   :    void CBoardCell2D::DrawBoard2D(Graphics* graphics)
* Description   :    Draw Board 2D.
*
* Parameters    :  "graphic"    [in,out] If non-null, the graphic.
* Return values :    true if it succeeds, false if it fails.
* 
* Mod. History  :    03/12/2012 - BaoTD1 (first created)
*
* --
*****************************************************************************/
BOOL CBoardCell2D::CreateBoardCell( const RECT& rect, CWnd* pParentWnd)
{
	///* Create window to draw board */
     if (!CWnd::Create(	NULL, 
						NULL, 
						WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_OWNERDRAW, 
						rect, 
						pParentWnd, m_nCellID, 
						NULL))
    {
        throw LPCSTR("CBoardCell2D::Create window fail !");
        return (FALSE);
    }

	this->m_ClientRect = rect;
	this->m_pParentWnd = pParentWnd;
	return TRUE;
}

/******************************************************************************
* ++
* Method name   :    void CBoardCell2D::DrawBoard2D(Graphics* graphics)
* Description   :    Draw Board 2D.
*
* Parameters    :  "graphic"    [in,out] If non-null, the graphic.
* Return values :    None
* 
* Mod. History  :    03/12/2012 - BaoTD1 (first created)
*
* --
*****************************************************************************/
void CBoardCell2D::OnMouseMove(UINT wParam, CPoint pos)
{
	if(!m_bClicked)
	{
		m_CellColor		= Color::Aquamarine;
		m_szCellImage	= FOLDERIMAGE_FOCUS;
		Invalidate(0);
	}
}

void CBoardCell2D::OnLButtonDown(UINT nFlags, CPoint point)
{
	if(m_bClicked)
	{
		m_CellColor				= Color::LightCyan;
		m_szCellImage			= FOLDERIMAGE_INACTIVE;
		m_bClicked				= FALSE;
		m_ClientRect.bottom		-= 70;
	}
	else
	{
		m_CellColor				= Color::Aquamarine;
		m_szCellImage			= FOLDERIMAGE_ACTIVE;
		m_bClicked				= TRUE;
		m_ClientRect.bottom		+= 70;
	}

	this->MoveWindow(&m_ClientRect);

	((CFolderTree2D*)m_pParentWnd)->CalculateFolderTree(m_nCellID);
}
